package com.oba.base.dao.pager;

import java.io.IOException;
import java.util.Map;

import javax.faces.application.Application;
import javax.faces.component.UIComponent;
import javax.faces.component.UIParameter;
import javax.faces.context.FacesContext;

import org.w3c.dom.Element;

import com.icesoft.faces.component.datapaginator.PaginatorActionEvent;
import com.icesoft.faces.component.ext.HtmlCommandLink;
import com.icesoft.faces.component.ext.HtmlInputText;
import com.icesoft.faces.context.DOMContext;
import com.icesoft.faces.renderkit.dom_html_basic.DomBasicRenderer;
import com.icesoft.faces.renderkit.dom_html_basic.PassThruAttributeRenderer;

public class PagerRender extends DomBasicRenderer {
	public boolean getRendersChildren() {
		return true;
	}

	public void decode(FacesContext context, UIComponent component) {
		validateParameters(context, component, UIPager.class);
		Map parameter = context.getExternalContext().getRequestParameterMap();
		String param = (String) parameter.get(component.getClientId(context));
		if (param != null && param.length() >= PAGE_NAVIGATION.length()) {
			if (param.equals(UIPager.FACET_GOTO)) {
				String pageIndexStr = (String) parameter.get(component
						.getClientId(context)
						+ PAGE_NAVIGATION);
				try {
					component.queueEvent(new PaginatorActionEvent(component,
							Integer.parseInt(pageIndexStr)));
				} catch (Exception e) {
				}
			} else
				component
						.queueEvent(new PaginatorActionEvent(component, param));
		} else {
//			component.queueEvent(new DelayActionEvent(
//					new MemoryPagerAdjustEvent(component), 1));
		}
	}

	public void encodeBegin(FacesContext facesContext, UIComponent uiComponent)
			throws IOException {
		validateParameters(facesContext, uiComponent, UIPager.class);
		UIPager scroller = (UIPager) uiComponent;
		scroller.getChildren().clear();
		setVariables(facesContext, scroller);
	}

	public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
			throws IOException {
		validateParameters(facesContext, uiComponent, UIPager.class);
		renderScroller(facesContext, uiComponent);
		removeVariables(facesContext, uiComponent);
	}

	protected void renderScroller(FacesContext facesContext,
			UIComponent uiComponent) throws IOException {
		UIPager scroller = (UIPager) uiComponent;
		if (scroller.getPager().getPageCount() <= 1)
			return;
		if (scroller.getFacets().size() <= 0)
			return;
		DOMContext domContext = DOMContext.attachDOMContext(facesContext,
				scroller);
		Element table;
		if (!domContext.isInitialized()) {
			table = domContext.createRootElement("table");
			setRootElementId(facesContext, table, scroller);
//			if (PassThruAttributeRenderer.passThruAttributeExists(scroller))
//				PassThruAttributeRenderer.renderAttributes(facesContext,
//						scroller, null);
		}
		table = (Element) domContext.getRootNode();
		DOMContext.removeChildren(table);
		Element tr = domContext.createElement("tr");
		table.appendChild(tr);
		String styleClass = scroller.getStyleClass();
		table.setAttribute("class", styleClass);
		String style = scroller.getStyle();
		if (style != null && style.length() > 0)
			table.setAttribute("style", style);
		else
			table.removeAttribute("style");
		String scrollButtonCellClass = scroller.getscrollButtonCellClass();

		UIComponent facetComp = scroller.getInfo();
		if (facetComp != null) {
			Element td = domContext.createElement("td");
			td.setAttribute("class", scrollButtonCellClass);
			tr.appendChild(td);
			domContext.setCursorParent(td);
			renderInfo(facesContext, scroller, facetComp);
		}

		facetComp = scroller.getFirst();
		if (facetComp != null) {
			Element td = domContext.createElement("td");
			td.setAttribute("class", scrollButtonCellClass);
			tr.appendChild(td);
			domContext.setCursorParent(td);
			renderFacet(facesContext, scroller, facetComp, UIPager.FACET_FIRST);
		}

		facetComp = scroller.getPrevious();
		if (facetComp != null) {
			Element td = domContext.createElement("td");
			td.setAttribute("class", scrollButtonCellClass);
			tr.appendChild(td);
			domContext.setCursorParent(td);
			renderFacet(facesContext, scroller, facetComp,
					UIPager.FACET_PREVIOUS);
		}

		facetComp = scroller.getNext();
		if (facetComp != null) {
			Element td = domContext.createElement("td");
			td.setAttribute("class", scrollButtonCellClass);
			tr.appendChild(td);
			domContext.setCursorParent(td);
			renderFacet(facesContext, scroller, facetComp, UIPager.FACET_NEXT);
		}
		facetComp = scroller.getLast();
		if (facetComp != null) {
			Element td = domContext.createElement("td");
			td.setAttribute("class", scrollButtonCellClass);
			tr.appendChild(td);
			domContext.setCursorParent(td);
			renderFacet(facesContext, scroller, facetComp, UIPager.FACET_LAST);
		}
		// input pageIndex begin
		facetComp = scroller.getGoto();
		if (facetComp != null) {
			Element td = domContext.createElement("td");
			td.setAttribute("class", scrollButtonCellClass);
			tr.appendChild(td);
			domContext.setCursorParent(td);
			getInput(facesContext, scroller).encodeAll(facesContext);
			renderFacet(facesContext, scroller, facetComp, UIPager.FACET_GOTO);
		}

		// input pageIndex end

		domContext.stepOver();

	}

	private void renderInfo(FacesContext facesContext, UIPager scroller,
			UIComponent facetComp) throws IOException {
		facetComp.encodeBegin(facesContext);
		if (facetComp.getRendersChildren())
			facetComp.encodeChildren(facesContext);
		facetComp.encodeEnd(facesContext);
	}

	protected void renderFacet(FacesContext facesContext, UIPager scroller,
			UIComponent facetComp, String facetName) throws IOException {
		HtmlCommandLink link = getLink(facesContext, scroller, facetName);
		if (scroller.getPager().getPageCount() <= 1)
			link.setDisabled(true);
		else
			link.setDisabled(false);
		link.encodeBegin(facesContext);
		facetComp.encodeBegin(facesContext);
		if (facetComp.getRendersChildren())
			facetComp.encodeChildren(facesContext);
		facetComp.encodeEnd(facesContext);
		link.encodeEnd(facesContext);
	}

	private HtmlCommandLink getLink(FacesContext facesContext,
			UIPager scroller, String facetName) {
		HtmlCommandLink link;
		Application application = facesContext.getApplication();
		link = (HtmlCommandLink) application
				.createComponent("com.icesoft.faces.HtmlCommandLink");
		link.setId(scroller.getId() + facetName);
		link.setTransient(false);
		UIParameter parameter = (UIParameter) application
				.createComponent("javax.faces.Parameter");
		parameter.setId(scroller.getId() + facetName + "_param");
		parameter.setTransient(false);
		parameter.setName(scroller.getClientId(facesContext));
		parameter.setValue(facetName);
		link.getChildren().add(parameter);
		scroller.getChildren().add(link);
		return link;
	}

	private HtmlInputText getInput(FacesContext facesContext, UIPager scroller) {
		HtmlInputText input;
		Application application = facesContext.getApplication();
		input = (HtmlInputText) application
				.createComponent("com.icesoft.faces.HtmlInputText");
		input.setId(scroller.getId() + PAGE_NAVIGATION);
		input.setSize(1);
		input.setTransient(false);
		scroller.getChildren().add(input);
		return input;
	}

	protected void setVariables(FacesContext facescontext, UIPager scroller)
			throws IOException {
		Map requestMap = facescontext.getExternalContext().getRequestMap();
		String pageVar = scroller.getPagerVar();
		if (pageVar != null) {
			requestMap.put(pageVar, scroller.getPager());
		}

	}

	public void removeVariables(FacesContext facescontext,
			UIComponent uiComponent) throws IOException {
		UIPager scroller = (UIPager) uiComponent;
		Map requestMap = facescontext.getExternalContext().getRequestMap();
		String pageVar = scroller.getPagerVar();
		if (pageVar != null)
			requestMap.remove(pageVar);
	}

	protected static final String PAGE_NAVIGATION = "idx".intern();

}
